package com.businessportal.web.dataaccess.services;

import com.businessportal.web.common.CryptoHelper;
import com.businessportal.web.dataaccess.dao.AddressDao;
import com.businessportal.web.dataaccess.dao.IAddressDao;
import com.businessportal.web.dataaccess.dao.IUserDao;
import com.businessportal.web.dataaccess.dao.UserDao;
import com.businessportal.web.dataaccess.entities.*;
import com.businessportal.web.dataaccess.utils.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.Date;
import java.util.List;


/**
 * Created by Vadim on 6/20/14.
 */
public enum UserService {

    INSTANCE;

    private IUserDao dao = new UserDao();
    private IAddressDao addrDao = new AddressDao();

    private UserService(){}

    public Login AddNewUser(String login, String password, String fullName) throws Exception {

        Login newUser = new Login();
        newUser.setLogin(login);
        newUser.setPassword(password);
        newUser.setFullName(fullName);
        dao.save(newUser);

        return newUser.getId() != null && newUser.getId() > 0 ? newUser : null;
    }

    public boolean IsUserNameAcceptable(String login){
        return dao.IsUserNameAcceptable(login);
    }

    public Login CheckLoginPass(String login, String pass){
        return dao.CheckLoginPass(login, pass);
    }

    public void AddPhone(Login user, String phone){
        user.addPhone(new Phone(phone));
        dao.save(user);
    }

    public void AddEmail(Login user, String email){
        user.addEmail(new Email(email));
        dao.save(user);
    }

    public void AddAddress(Login user, String postalCode, String country, String city, String street){
        user.addAddress(new Address(postalCode, country, city, street));
        dao.update(user);
    }

    public void RemoveAddress(Login user, long addresId){
        user.removeAddressById(addresId);
        addrDao.deleteById(addresId);
    }

    public void UpdateUser(Login user){
        dao.update(user);
    }

    public Login GetLoginByEmail(String login){
        return dao.GetLoginByEmail(login);
    }

    public Login GetLoginById(long id){
        return dao.getById(id);
    }
}
